package com.example.penggo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast; 
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.view.View;


import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.signpost.http.HttpResponse;
 
public class HttpAPITestActivity extends Activity {  
    private final static String TAG="HttpAPITestActivity";  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        Log.v(TAG, "onCreate");  
        setContentView(R.layout.activity_httpapi);  

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads()  
                .detectDiskWrites().detectNetwork().penaltyLog().build());       
        
        Button btnExit=(Button)findViewById(R.id.test);  

        btnExit.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
//                finish();
            		
                MyThread myThread1 = new MyThread();   
                myThread1.start(); 
       	
            }  
        }); 
        
        
        

   	
    }  
    
    class MyThread extends Thread {  
  
        public void run() {  
			try {
            	// http地址  
                String httpUrl = "https://api.douban.com/v2/book/user/57952262/collections";  
                //HttpGet连接对象  
                HttpGet httpRequest = new HttpGet(httpUrl);  
             //取得HttpClient对象  
                HttpClient httpclient = new DefaultHttpClient();  
                //请求HttpClient，取得HttpResponse  
                HttpResponse httpResponse;						
				httpResponse = (HttpResponse) httpclient.execute(httpRequest);
                //请求成功  
                if (((org.apache.http.HttpResponse) httpResponse).getStatusLine().getStatusCode() == HttpStatus.SC_OK)  
                {   
                    //取得返回的字符串  
                    String strResult = EntityUtils.toString(((org.apache.http.HttpResponse) httpResponse).getEntity());  
                    JSONArray jsonArray;
					try {
						JSONObject item = new JSONObject(strResult);
						int	length = item.length();
						
							
						jsonArray = item.names();
                        for (int i = 0; i < jsonArray.length(); i++) { 

	                        String name = jsonArray.getString(i); //每条记录又由几个Object对象组成 
	                        Log.i(TAG,name);  
                        
                        }	
	                        							
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} //数据直接为一个数组形式，所以可以直接 用android提供的框架JSONArray读取JSON数据，转换成Array 


                }  
                else 
                {  

                }   
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
        }  
    }  
    

    @Override  
    protected void onStart() {  
        super.onStart();  
        Log.v(TAG, "onStart");  
    }  
    @Override  
    protected void onResume() {  
        super.onResume();  
        Log.v(TAG, "onResume");  
    }  
    @Override  
    protected void onPause() {  
        super.onPause();  
        Log.v(TAG, "onPause");  
    }  
    @Override  
    protected void onStop() {  
        super.onStop();  
        Log.v(TAG, "onStop");  
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        Log.v(TAG, "onDestroy");  
    }  
      
} 